<?php

namespace App\Http\Controllers\Goods;

use App\Models\Attr;
use App\Models\Goods;
use App\Models\GoodsAttr;
use App\Models\Huopin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HuopinController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // 获取商品的id
        $id=$request->query('goods');
        // 获取id所对应的商品
        $goods=Goods::find($id);
        // 获取当前当前id商品的所有货品的数据
        $huopins=Huopin::where('goods_id',$id)->get();

        return view('huopin.index',compact('goods','huopins'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        // 获取要添加货品的商品id
        $id=$request->query('goods');
        // 获取id所对应的商品
        $goods=Goods::find($id);
        // 获取商品属性表中对应id的所有顶级属性id
        $topIds=GoodsAttr::where('goods_id',$id)->pluck('attr_id');
        // 获取对应顶级属性
        $topAttr=Attr::whereIn('id',$topIds)->get();

        return view('huopin.create',compact('topAttr','goods','id'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 将post数据存入货品数据
//        dd($request->all());
        Huopin::create($request->all());
        return redirect()->route('goods.huopin.index',['goods'=>$request->goods_id])->with('success','货品添加成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Huopin  $huopin
     * @return \Illuminate\Http\Response
     */
    public function show(Huopin $huopin)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Huopin  $huopin
     * @return \Illuminate\Http\Response
     */
    public function edit(Huopin $huopin ,Request $request)
    {
        // 获取要编辑货品的商品id
        $id=$request->query('goods');
        // 获取id所对应的商品
        $goods=Goods::find($id);
        // 获取商品属性表中对应id的所有顶级属性id
        $topIds=GoodsAttr::where('goods_id',$id)->pluck('attr_id');
        // 获取对应顶级属性
        $topAttr=Attr::whereIn('id',$topIds)->get();
        return view('huopin.edit',compact('goods','topAttr','id','huopin'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Huopin  $huopin
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Huopin $huopin)
    {
        // 将post数据中的货品数据对对应的货品进行修改
        $huopin->update($request->all());

        // 提示信息并返回
        return redirect()->route('goods.huopin.index', ['goods' => $request->goods_id])->with('success', '货品编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Huopin  $huopin
     * @return \Illuminate\Http\Response
     */
    public function destroy(Huopin $huopin)
    {
        $huopin->delete();
        return back()->with('success','货品删除成功');
    }
}
